#include "mywidget.h"
#include "ui_mywidget.h"

MyWidget::MyWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::MyWidget)
{
    ui->setupUi(this);
    initital();
}

MyWidget::~MyWidget()
{
    delete ui;
}

void MyWidget::initital()
{
    my_DB = new test15_DB;

    connect(my_DB,&test15_DB::sig_over,this,&MyWidget::slot_over);
    connect(my_DB,&test15_DB::sig_process,this,[=](int process)
    {
       ui->progressBar->setValue(process);
    });
}

bool test15_DB::rqlint(QString dbname)
{
    DB=QSqlDatabase::addDatabase("QSQLITE"); //添加 SQL LITE数据库驱动
    DB.setDatabaseName(dbname); //设置数据库名称

    if (!DB.open())   //打开数据库
    {
        return 0;
    }
    return 1;
}

void test15_DB::creattable(QString tablename)
{
    QString creatquery;
    creatquery = "CREATE TABLE \""+tablename+"\"(\"序号\" integer NOT NULL,"
                                             "\"随机数1\" integer,"
                                             "\"随机数2\" integer,"
                                             "\"随机数3\" integer,"
                                             "PRIMARY KEY (\"序号\"))";
    QSqlQuery sql_query;
    sql_query.prepare(creatquery);
    sql_query.exec();
    start();
}

void test15_DB::initial_data()
{
    for(int i=0;i<1000;i++)
    {
        QString sql_str = QString("INSERT INTO \"数据库表1\"(\"序号\", \"随机数1\", \"随机数2\", \"随机数3\") "
                                    "VALUES (%1,%2,%3,%4)").arg(i+1).arg(qrand()%3000).arg(qrand()%3000).arg(qrand()%3000);
        QSqlQuery sql_query;
        sql_query.prepare(sql_str);
        sql_query.exec();
        if(!(i&10))
        {
            emit sig_process(i/10);
        }
    }
}

void test15_DB::select(QString name)
{
    tabModel=new QSqlTableModel(nullptr,DB);//数据表
    tabModel->setTable(name); //设置数据表
    if (!(tabModel->select()))//查询数据
    {
       return;
    }

//字段显示名
    tabModel->setHeaderData(tabModel->fieldIndex("序号"),Qt::Horizontal,"序号");
    tabModel->setHeaderData(tabModel->fieldIndex("随机数1"),Qt::Horizontal,"随机数1");
    tabModel->setHeaderData(tabModel->fieldIndex("随机数2"),Qt::Horizontal,"随机数2");
    tabModel->setHeaderData(tabModel->fieldIndex("随机数3"),Qt::Horizontal,"随机数3");
}

void test15_DB::deletedata(int counm , QString nametable)
{
    QString name;
    if(counm)
        name = "随机数"+QString::number(counm);
    else
        name = "序号";
    QString sql_str = QString("DELETE FROM %1 WHERE NOT %2 % 2").arg(nametable).arg(name);
    QSqlQuery sql_query;
    sql_query.prepare(sql_str);
    sql_query.exec();
}

void MyWidget::on_pushButton_3_clicked()
{
    my_DB->rqlint(ui->lineEdit->text());
}

void MyWidget::on_pushButton_clicked()
{
    ui->label_state->setText("正在生成");
    ui->progressBar->setValue(0);
    my_DB->creattable(ui->lineEdit_2->text());
    set_enable(false);
}

void MyWidget::slot_over(int error)
{
    ui->label_state->setText("");
    ui->progressBar->setValue(100);
    set_enable(true);
}

void MyWidget::set_enable(bool state)
{
    ui->pushButton->setEnabled(state);
    ui->pushButton_2->setEnabled(state);
    ui->pushButton_4->setEnabled(state);
    ui->pushButton_6->setEnabled(state);
}

void test15_DB::run()
{

    initial_data();
    sig_over(0);
}

void MyWidget::on_pushButton_6_clicked()
{
    my_DB->select(ui->lineEdit_2->text());
    ui->tableView->setModel(my_DB->tabModel);
}

void MyWidget::on_pushButton_2_clicked()
{
    my_DB->tabModel->sort(ui->spinBox->value(),Qt::DescendingOrder);
}

void MyWidget::on_pushButton_4_clicked()
{
    QString data;
    QTextStream out(&data);
    QString nametable = ui->lineEdit_2->text();
    QString name;
    int count = ui->spinBox_2->value();
    if(count)
        name = "随机数"+QString::number(count);
    else
        name = "序号";
    my_DB->deletedata(count,nametable);
    QSqlQuery sql_query;
    sql_query.exec(QString("SELECT * FROM %1 WHERE %2 % 2").arg(nametable).arg(name));
    while (sql_query.next()) {
        out<<sql_query.value(0).toInt()<<","<<sql_query.value(1).toInt()
          <<","<<sql_query.value(2).toInt()<<","<<sql_query.value(3).toInt()<<"\n";
    }
    ui->textEdit->setText(data);
}
